package net.mihome.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.ImageLoader;

import net.mihome.app.AppContext;
import net.mihome.app.R;
import net.mihome.app.bean.BuyCar;
import net.mihome.app.bean.Goods;
import net.mihome.app.bean.URLs;
import net.mihome.app.bean.net.ResponsePojo;
import net.mihome.app.common.UIHelper;
import net.mihome.app.ui.view.MyViewLinster;
import net.mihome.app.utils.MiAsyncHttpResponseHandler;
import net.mihome.app.utils.RequestParamsFactory;

import org.apache.http.Header;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 购物车适配器
 * */
public class BuyCarAdapter extends ArrayAdapter<BuyCar> {

    private static final String TAG = "SampleAdapter";
    private LayoutInflater listContainer;
	 private List<BuyCar> listItems=new ArrayList<BuyCar>();
    private ImageLoader imageLoader;
	 private Context context;
    private MyViewLinster myViewLinster;
    public static class ViewClass{
       ImageView ivImageView;
        TextView nameTextView;
        TextView descTextView;
        TextView priceTextView;
        TextView priceUnitTextView;
        TextView buyNumTextView;
        Button buyNumDel;
        Button buyNumAdd;
        Button buyCarImageView;
    }
    public BuyCarAdapter(Context context, int resource, List<BuyCar> objects, ImageLoader imageLoader, MyViewLinster myViewLinster) {
		super(context, resource, objects);
        this.context=context;
		listContainer = LayoutInflater.from(context);
		listItems=objects;
        this.myViewLinster=myViewLinster;
        this.imageLoader=imageLoader;
		// TODO Auto-generated constructor stub
	}
    public BuyCarAdapter(final Context context, final int textViewResourceId) {
        super(context, textViewResourceId);
        this.context=context;
        listContainer = LayoutInflater.from(context);
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
         View  v = listContainer.inflate(R.layout.goods_listitem, parent, false);
        if(v.getTag()==null){
            ViewClass viewClass=new ViewClass();
            viewClass.nameTextView=(TextView) v.findViewById(R.id.goods_name);
            viewClass.descTextView=(TextView) v.findViewById(R.id.goods_desc);
            viewClass.priceTextView=(TextView) v.findViewById(R.id.good_price);
            viewClass.priceUnitTextView=(TextView) v.findViewById(R.id.good_price_unit);
            viewClass.buyNumTextView= (TextView) v.findViewById(R.id.goods_buy_num);
            viewClass.ivImageView=(ImageView) v.findViewById(R.id.goods_image);
            viewClass.buyNumAdd= (Button) v.findViewById(R.id.goods_buy_add);
            viewClass.buyNumDel= (Button) v.findViewById(R.id.goods_buy_del);
            viewClass.buyCarImageView= (Button) v.findViewById(R.id.goods_buy_icon);
            viewClass.buyCarImageView.setText("更新购物车");
            viewClass.buyCarImageView.setTag(listItems.get(position));
            addToBuyCar(viewClass.buyCarImageView, viewClass.buyNumTextView);
            v.setTag(viewClass);
        }
        BuyCar buyCar=listItems.get(position);
        if(buyCar.getGoods()==null){
            Toast.makeText(context,"该商品不存在",Toast.LENGTH_SHORT).show();
        }
        ViewClass viewClass= (ViewClass) v.getTag();
         if(buyCar.getGoods().getImg()!=null&&!"".equals(buyCar.getGoods().getImg())){
             imageLoader.displayImage(buyCar.getGoods().getImg(),viewClass.ivImageView);
         }else{
             viewClass.ivImageView.setImageResource(R.drawable.ic_menu_picnoshow);
         }
        viewClass.nameTextView.setText(buyCar.getGoods().getName());

        viewClass.descTextView.setText(buyCar.getGoods().getDesc());

        viewClass.priceTextView.setText(buyCar.getGoods().getPrice()+"");
        viewClass.buyNumTextView.setText(buyCar.getNum()+"");
        viewClass.priceUnitTextView.setText("元/"+buyCar.getGoods().getUnit());
        addBuyNumLinstener(viewClass.buyNumAdd, viewClass.buyNumTextView, true);
        addBuyNumLinstener(viewClass.buyNumDel, viewClass.buyNumTextView, false);
         return v;
    }
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listItems.size();
	}
	@Override
	public BuyCar getItem(int position) {
		// TODO Auto-generated method stub
		return listItems.get(position);
	}
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return listItems.get(position).getId();
	}
    /**
     * 点击拨打电话图标
     * */
    public View.OnClickListener onCllIconClickListener=new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Goods goods= (Goods) view.getTag();
            String phoneNums=goods.getPhoneNum();
            // TODO Auto-generated method stub
            if(phoneNums!=null&&!"".equals(phoneNums)){
                String[] items=phoneNums.split("、|,");
                boolean[] isCheck={true,false};
                UIHelper.simpleDialog(context, items);
            }else{
                Toast.makeText(context,"暂无电话",Toast.LENGTH_LONG).show();
            }
        }
    };
    public void addBuyNumLinstener(Button button, final TextView textView, final boolean isAdd){
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(textView.getText().toString()!=null){
                    int num=Integer.parseInt(textView.getText().toString());
                    if(isAdd){
                        num++;
                    }else{
                        num--;
                    }
                    if(num<0){
                        num=0;
                    }
                    textView.setText(num+"");
                }
            }
        });
    }
    /**
     * 添加到购物车中
     * */
    public void addToBuyCar(View view,final TextView textView){
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String numString=textView.getText().toString();
                if(numString!=null){
                    Goods goods= ((BuyCar) view.getTag()).getGoods();
                    RequestParams requestParams=RequestParamsFactory.newInstance(AppContext.getInstance());
                    requestParams.add("goods_id",goods.getId()+"");
                    requestParams.add("num",numString);
                    AppContext.ASYNC_HTTP_CLIENT.get(context,URLs.MIHOME_GET_GOODS_BUYCAR_RESET_NUM,requestParams,new MiAsyncHttpResponseHandler<Map>() {
                        @Override
                        public void onSuccess(int arg0, Header[] arg1, ResponsePojo<Map> responsePojo) {
                            Toast.makeText(context,"操作成功",Toast.LENGTH_SHORT).show();
                            if(myViewLinster!=null){
                                myViewLinster.onLoadComplete();
                            }
                        }

                        @Override
                        public void onFailure(int arg0, Header[] arg1, Throwable arg3) {
                            Toast.makeText(context,"操作失败"+arg3.getMessage(),Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        });
    }
}
